<htm>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="public/css/css.css">
        <title> HOAI TAM </title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body
    
        <?php
        require_once 'model/dbs.php';
        ?>

        <h1> HOAI TAM </h1>
        <?php
        $query = "SELECT * FROM companies";
        $companies = getData($query);
        ?>
        <?php
        $query = "SELECT * FROM apps";
        $apps = getData($query);
        ?>

        <form method="POST">
            Min fans : &nbsp;&nbsp;&nbsp; <input type="text" name="min_fans"value="1" id ="min_fans"><br>
            Max fans : &nbsp;&nbsp;&nbsp; <input type="text" name="max_fans"value="10"><br>
            Keyword  : &nbsp;&nbsp;&nbsp; <input type="text" name="keyword"value="cafe"><br>
            App : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<select name="id_apps">
                <option value="0">select app</option>
                <?php foreach ($apps as $app): ?>
                    <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                <?php endforeach; ?>

        </form>
        </select> <br>
        Company &nbsp; &nbsp;<select name="id_company">
            <option value="0">select company</option>
            <?php foreach ($companies as $company): ?>
                <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
            <?php endforeach; ?>
        </select>
        <input type="submit" name="submit">
        
        <input type="button" id="check" value="Button">
        </form>
        </select> <br>


        <?php if (isset($_POST['submit'])): ?>
            <?php
            $min_fans = $_POST['min_fans'];
            $max_fans = $_POST['max_fans'];
            $keyword = $_POST['keyword'];
            $company = $_POST["id_company"];
            $apps = $_POST["id_apps"];
            if (!empty($company)) //empty ktra rong kg.
                $company = "(companies.id=$company) AND";
            else
                $company = "";
            if (!empty($apps))
                $apps = "(apps.id=$apps) AND";
            else
                $apps = "";

            if (empty($max_fans) && !empty($min_fans)) {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps 
                  (pages.likes>= $min_fans) and
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
            } else if (!empty($max_fans) && empty($min_fans)) {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  (pages.likes<= $max_fans) and
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
            } else if (empty($max_fans) && empty($min_fans)) {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
            } else {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  (pages.likes>= $min_fans) and
                  (pages.likes<= $max_fans) and
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
            }
            var_dump($query);
            $pages = getData($query);
            //var_dump($pages);
            ?>
        <?php endif; ?>
        <?php if (!empty($pages)): ?>
            <table id="khung">
                <tr >
                    <td id="khung"> Profile </td>
                    <td id="khung">Name</td>

                    <td id="khung">Phone</td>
                    <td id="khung"> Likes </td>
                    <td id="khung"> Description </td>
                </tr>
                <?php foreach ($pages as $page): ?>
                    <tr>
                        <td id="khung"><img src="<?php echo $page['picture'] ?>"></td>
                        <td id="khung"><?php echo $page['name'] ?></td>

                        <td id="khung"><?php echo $page['phone'] ?></td>
                        <td id="khung"><?php echo $page['likes'] ?></td>
                        <td id="khung"><?php echo substr($page['description'], 0, 100) ?></td>
                        <?php //var_dump($page);die(); ?>

                    </tr>
                <?php endforeach; ?>
            </table> 

        <?php endif; ?>
        <script type ="text/javascript">
            var txt = Document.getElementById ('min_fans').value;
        alert("Hello Word" + txt );
        </script>
        <script>
            $("#min_fans").click(function (){
                alert($("#min_fans")).value()
            });
        </script>
        <script>
            $(document).ready(function (){
                $('#check').click(function (){
                    var min_fans = $('#min_fans').val();
                    var flag = checkNumber (min_fans);
                    if (flag){
                        alert('Is number');
                    } else{
                        alert(' Is not number');
                    }
                });
                function  checkNumber (val){
                    if($.isNumeric(val)){
                        return true;
                    }else{
                        return false;
                    }
                
                }
               
            });
        </script>

        </body>
</htm>
